<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class slideshow_controller extends admin_controller
{
    // Get list
    public function __construct()
    {
        parent::__construct();
        if (!$this->is_admin_logged()) redirect(base_url() . $this->base_admin_url . 'login_controller');
        $this->page_title = 'User';
        $this->table_name = 'user';
        $this->route_name = 'slideshow_controller';
        $this->business_model = $this->slideshow_model;
    }

    public function index()
    {
        $header_data = array();
        $header_data['show_menu'] = true;
        $view_data = $this->get_view_data();
        $view_data['list'] = $this->get_list();
        $view_data['actions'] = $this->get_list_actions();
        $this->load_view('list_template', $header_data, $view_data);
    }

    public function delete()
    {
        if ($this->input->post('selected') === false) redirect(base_url() . $this->base_admin_url . $this->route_name);
        $selected = $this->input->post('selected');
        foreach ($selected as $id)
        {
            // Delete news
            if ($this->get_logged_admin_account() != $id)
                $this->business_model->delete_data(array('id' => $id));
        }
        $this->session->set_userdata('message', 'Hoàn tất: Bạn đã xóa dữ liệu!');
        redirect(base_url() . $this->base_admin_url . $this->route_name);
    }

    public function get_list()
    {
        $list = array();
        $news = $this->business_model->get_elements();
        //        echo json_encode($news);exit;
        foreach ($news as $item)
        {
            $actions = array();
            $actions[] = array(
                'text' => 'Sửa',
                'href' => base_url() . $this->base_admin_url . $this->route_name . '/update/' . $item->id
            );

            $list[] = array(
                'can_delete' => $item->id,
                'id' => $item->id,
                'image_path' => '<img src="' . base_url() . $item->image_path . '" alt="sfdsfd" width="100px" height="100px">',
                'modified_date' => date('d-m-Y H:i:s', $item->modified_date),
                'actions' => $actions
            );
        }
        return $list;
    }

    public function get_header_list()
    {
        $list = array();
        $list[] = 'ID';
        $list[] = 'Hình';
        $list[] = 'Chỉnh sửa';
        $list[] = 'Hành động';
        return $list;

    }

    public function get_list_actions()
    {
        $actions = array();
        $actions[] = array(
            'text' => 'Thêm',
            'href' => base_url() . $this->base_admin_url . $this->route_name . '/update/-1',
            'view_change_method' => 'redirect'
        );
        $actions[] = array(
            'text' => 'Xóa',
            'href' => base_url() . $this->base_admin_url . $this->route_name . '/delete',
            'view_change_method' => 'submit'
        );
        return $actions;
    }

    public function get_post_data()
    {
        $image_path = $this->upload_image('image_path', $this->input->post('old_path'));
        $this->business_model->title_en = $this->input->post('title_en');
        $this->business_model->title_vi = $this->input->post('title_vi');
        $this->business_model->description_en = $this->input->post('description_en');
        $this->business_model->description_vi = $this->input->post('description_vi');
        $this->business_model->image_path = $image_path;
        $this->business_model->modified_date = time();
    }

    public function get_fields($id)
    {
        if ($id == -1)
            $model = new $this->business_model();
        else
            $model = $this->business_model->get_element_by_id($id);

        $fields = array();

//        $fields[] = array(
//            'text' => 'Tiêu đề (Anh)',
//            'name' => 'title_en',
//            'value' => $model->title_en,
//            'type' => 'text'
//        );

//        $fields[] = array(
//            'text' => 'Tiêu đề (Việt)',
//            'name' => 'title_vi',
//            'value' => $model->title_vi,
//            'type' => 'text'
//        );
//        $fields[] = array(
//            'text' => 'Mô tả (Anh)',
//            'name' => 'description_en',
//            'value' => $model->description_en,
//            'type' => 'textarea'
//        );

//        $fields[] = array(
//            'text' => 'Mô tả (Việt)',
//            'name' => 'description_vi',
//            'value' => $model->description_vi,
//            'type' => 'text'
//        );
        $fields[] = array(
            'text' => 'Hình hiện tại',
            'name' => '',
            'value' => '<img src="' . base_url() . $model->image_path . '" alt="" width="200px" height="200px">',
            'type' => 'label'
        );

        $fields[] = array(
            'text' => '',
            'name' => 'old_path',
            'value' => $model->image_path,
            'type' => 'hidden'
        );

        $fields[] = array(
            'text' => 'Upload hình',
            'name' => 'image_path',
            'value' => '',
            'type' => 'file'
        );
        return $fields;
    }

}
